<div class="jumbotron simple">
    <div class="container">
        <h1>Tài khoản <small>Quản lý email</small></h1>
    </div>
</div>
<div id="container-main" class="container">
    <div class="row">
        <div class="col-md-18 col-sm-14">

            <div class="page-header">
                <h3>
                    <i class="fa fa-envelope-o"></i> Danh sách email
                    <span class="page-header-under danger"></span>
                </h3>
            </div>

            <div id="email_list">
                <?php $this->renderPartial('manage_email_list', array('userEmails' => $this->user->userEmails))?>
            </div>

            <div class="row">
                <div class="col-md-12 marginbottom20">  
                    <div class="page-header">
                        <h3>
                            <i class="fa fa-plus-circle"></i> Thêm email
                            <span class="page-header-under primary"></span>
                        </h3>
                    </div>

                    <?php $form=$this->beginWidget('CActiveForm', array(
                            'id'=>'add-email-form',
                            'enableClientValidation'=>true,
                            'clientOptions'=>array(
                                'validateOnSubmit' => true,
                                'validateOnChange' => true,
                            ),
                            'enableAjaxValidation'=>false,
                            'htmlOptions' => array('class' => 'form-horizontal')
                        )); ?>

                    <div class="input-group">
                        <?php echo $form->textField($emailModel,'email',array('placeholder'=> $emailModel->getAttributeLabel('email'), 'maxlength'=>255, 'class' => 'form-control')); ?>
                        <span class="input-group-btn">
                            <button class="btn btn-primary" type="submit"><i class="fa fa-plus"></i> Thêm</button>
                        </span>
                    </div>

                    <?php echo $form->error($emailModel,'email');?>

                    <?php $this->endWidget(); ?>
                </div>

                <div class="col-md-12">  
                    <div class="page-header">
                        <h3>
                            <i class="fa fa-dot-circle-o"></i> Thêm đăng nhập
                            <span class="page-header-under success"></span>
                        </h3>
                    </div>
                    <div>
                        <a class="openid_popup decoration-none" rel="popover"  data-placement="top" data-trigger="hover" data-content="Thêm tài khoản Facebook" href="<?php echo $this->createUrl('/user/page/manageEmail', array('service' => 'facebook'))?>">
                            <i class="fa fa-facebook-square fa-5x text-primary"></i>
                        </a>

                        <a class="openid_popup decoration-none" rel="popover" data-placement="top" data-trigger="hover" data-content="Thêm tài khoản Google" href="<?php echo $this->createUrl('/user/page/manageEmail', array('service' => 'google'))?>">
                            <i class="fa fa-google-plus-square fa-5x text-danger"></i>
                        </a>
                    </div>

                </div>
            </div>
            <hr class="visible-xs"/>
        </div>
        <div class="col-md-6 col-sm-10">
            <?php $this->renderPartial('_menu');?>
        </div>
    </div>
</div>


<script>
    $('input[name="is_main"]').live('change', function(){
        var email = $(this).attr('email_value');
        $.post('<?php echo $this->createUrl('/user/page/ajaxUpdateEmailList')?>', {
            action: 'update_main', 
            email_id: $(this).val()
            }, function(data){
                $('#email_list').html(data);
                myGrowl('Cập nhật thành công '+email+' là email chính!', 'success');    
            }
        );    
    }); 

    $('.email_remove').live('click', function(e){
        e.preventDefault();
        var email = $(this).attr('email');
        var email_id = $(this).attr('email_id');

        bootbox.confirm({
            message: 'Bạn có chắc chắn muốn loại bỏ email '+ email + ' ?',
            buttons: {
                confirm: {
                    label: "<i class='fa fa-trash-o'></i> Chắc chắn xóa",
                    className: "btn-danger",
                },
                cancel: {
                    label: "Không xóa",
                    className: "btn-default",
                }
            },
            callback: function(result){
                if(result){
                    $.post('<?php echo $this->createUrl('/user/page/ajaxUpdateEmailList')?>', {
                        action: 'remove', 
                        email_id: email_id
                        }, function(data){
                            $('#email_list').html(data);
                            myGrowl('Loại bỏ Email '+email+' thành công!', 'info');    
                    });   
                }
            }
        });  

    });                                                                                                                                                 
</script>